package com.hk.service.impl.kyc;

import java.util.LinkedHashMap;

import org.hibernate.internal.util.StringHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Component;

import com.hk.commons.JsonUtils;
import com.hk.commons.RedisCilent;
import com.hk.commons.vo.CheckInfo;
import com.hk.commons.vo.CouponActMsg;
import com.hk.commons.vo.JsonResult;

@Component
public class CouponRespHandler {

	private Logger logger = LoggerFactory.getLogger(CouponRespHandler.class);
	
	 
	public void handler(Object obj) {
		 System.out.println("[x] received request for --object: " + obj);
	}
	
	public void handler(String obj) {
		 System.out.println("[x] received request for --String: " + obj);
	}
	
	public void handler(byte[]  msg) {
        
        String strMsg = new String(msg);
        
        System.out.println("[x] received request for --byte" + strMsg);
 
		logger.info("get mq result strMsg={}", strMsg);
		System.out.println("object = " + strMsg);
		
		if(StringHelper.isEmpty(strMsg)){
			logger.error("get mq result is null ");
			return;
		}
		
		JsonResult jsonResult = JsonUtils
				.toBean(strMsg, JsonResult.class);
		logger.info("get mq result strMsg={}", strMsg);
		
		String ssn = jsonResult.ssn;
		
		CheckInfo checkInfo = new CheckInfo();
		
		
		
		//首先判断是否成功
		if(jsonResult.resultCode.equals("200")){
			LinkedHashMap restHashMap  =   (LinkedHashMap) jsonResult.result;
			
			//成功则转型
			logger.info("get mq result restMessage={}", restHashMap.toString());
			
			logger.info("class name " + restHashMap.get("userCardId").getClass());
			
			Long userCardId = Long.valueOf(restHashMap.get("userCardId").toString());
		 
			Boolean actResult = (Boolean) restHashMap.get("actResult");
			
			
			logger.info("couponActMsg :  result = {}, actResult={}",  userCardId, actResult);
			
			RedisCilent.setString(ssn.toString(), actResult.toString(), 1000 * 60);
			
		}else{
			//不成功则报错
			logger.error("get mq result error restMessage={}", jsonResult);
			
			RedisCilent.setString(ssn, "0", 1000 * 60);
			
		}
	}

}
